<template>
  <div>
    <div class="card" style="margin-bottom: 10px">
      <el-table :data="data.tableData">
        <el-table-column prop="id" label="订单号" width="300"/>
        <el-table-column prop="createTime" label="创建时间"/>
        <el-table-column prop="state" label="订单状态" :formatter="formatState"/>
        <el-table-column prop="foodNum" label="餐品总数"/>
        <el-table-column prop="totalPrice" label="餐品总价格"/>
        <el-table-column label="操作" width="180">
          <template #default="scope">
            <el-button type="primary" @click="viewDetail(scope.row)" >查看明细</el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <div class="card" v-if="data.total">
      <el-pagination background layout="prev, pager, next" @current-change="load" :page-size="data.pageSize" v-model:current-page="data.pageNum" :total="data.total"/>
    </div>

    <el-dialog v-model="data.formVisible" title="订单详情" width="60%" destory-on-close>
      <el-table :data="data.detailData" style="height: 500px">
        <el-table-column prop="id" label="序号" width="70"/>
        <el-table-column prop="name" label="名称"/>
        <el-table-column prop="descr" label="简介"/>
        <el-table-column prop="price" label="价格"/>
        <el-table-column label="图片">
          <template v-slot="scope">
            <el-image style="width: 100px; height: 100px; display: block" v-if="scope.row.img" :src="scope.row.img"
                      :preview-src-list="[scope.row.img]" preview-teleported></el-image>
          </template>
        </el-table-column>
        <el-table-column prop="num" label="数量"/>
      </el-table>
    </el-dialog>
  </div>
</template>

<script setup>
import {reactive} from "vue"
import request from "../../utils/request";
import {ElMessage, ElMessageBox} from "element-plus";

const data = reactive({
  tableData: [],
  detailData: [],
  total: 0,
  pageNum: 1,  // 当前的页码
  pageSize: 5,  // 每页的个数
  formVisible: false,
  form: {},
  name: '',
  user: JSON.parse(localStorage.getItem('canteen-user') || '{}')
})

const viewDetail = (row) =>{
  data.formVisible = true
  request.post('/orderDetail/queryAll', {
    orderId: row.id
  }).then(res => {

    if (res.code === 200) {  //成功
      data.detailData = res.data
    } else {
      ElMessage.error(res.msg)
    }
  })
}
    // , column, cellValue, index
const formatState = (row) => {
  const state = row.state
  if (state === '0') {
    return '进行中'
  } else if (state === '1') {
    return '已取消'
  } else if (state === '2') {
    return '已完成'
  }
}

const load = () =>{
  request.get('/order/selectPage', {
    params:{
      pageNum:data.pageNum,
      pageSize:data.pageSize,
      userId:data.user.id,
    }
  }).then(res => {
    data.tableData = res.data?.list || []
    data.total = res.data?.total || 0
  })
}
load()


</script>
